package com.zhentao.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.aop.MyLog;
import com.zhentao.domain.TUser;
import com.zhentao.domain.dto.AdTeacher;
import com.zhentao.domain.dto.TeacherDto;
import com.zhentao.domain.dto.UpTeacher;
import com.zhentao.domain.excel.TeacherExport;
import com.zhentao.result.Result;
import com.zhentao.service.FileStorageService;
import com.zhentao.service.SClassService;
import com.zhentao.service.TUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TUserService tUserService;

    @Resource
    private SClassService service;

    @Resource
    private   FileStorageService fileStorageService;

    //获取班级列表
    @PostMapping("/getClassList")
    public Result getClassList() {
        return Result.OK(service.list());
    }

    //获取教师列表
    @PostMapping("/getTeacherList")
    public Result getTeacherList(@RequestBody TeacherDto teacherDto) {
            return tUserService.getTeacherList(teacherDto);
    }

    //上传图片
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file){
        String filename = file.getOriginalFilename();//获取文件名
        String s= UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
        String s1 ="";
        try {
            s1=this.fileStorageService.uploadImgFile(s, s, file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.OK(s1);
    }



    //新增教师
    @PostMapping("/addTeacher")
    @MyLog(title = "教师管理", content = "新增讲师")
    public Result addTeacher(@RequestBody AdTeacher adTeacher) {
        return tUserService.addTeacher(adTeacher);
    }

    //根据主键查讲师
    @PostMapping("/findOneTeacher/{uid}")
    public Result findOneTeacher(@PathVariable("uid") Integer uid) {
        return tUserService.findOneTeacher(uid);
    }


    //修改讲师
    @PostMapping("/updateTeacher")
    @MyLog(title = "教师管理", content = "修改讲师")
    public Result updateTeacher(@RequestBody UpTeacher upTeacher){
        return tUserService.updateTeacher(upTeacher);
    }


    //禁用讲师
    @PostMapping("/disableTeacher/{uid}")
    @MyLog(title = "教师管理", content = "禁用讲师")
    public Result disableTeacher(@PathVariable("uid") Integer uid) {
        return tUserService.disableTeacher(uid);
    }



    //删除讲师
    @PostMapping("/deleteTeacher/{uid}")
    @MyLog(title = "教师管理", content = "删除讲师")
    public Result deleteTeacher(@PathVariable("uid") Integer uid) {
        return tUserService.deleteTeacher(uid);
    }


    //重置讲师密码
    @GetMapping("/resetPassword/{uid}")
    @MyLog(title = "教师管理", content = "重置讲师密码")
    public Result resetTeacherPassword(@PathVariable("uid") Integer uid) {
        return tUserService.resetTeacherPassword(uid);
    }

     //导出教师信息
    @GetMapping("/exportFile")
    @MyLog(title = "教师管理", content = "导出讲师信息")
    public void export(HttpServletResponse response) throws IOException {
        List<TeacherExport> list = this.tUserService.getTeacherListExport();
        response.setContentType("application/vnd.openxmlformats-officedocument,spreadsheetml,sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("讲师基本信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TeacherExport.class).sheet("讲师基本信息").doWrite(list);
    }


}
